#include "bits/stdc++.h"
#include "GBinaraySearchTree.h"

int main() {
    GBinaraySearchTree bTree;
    std::vector<int> list;
    int n;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        int value;
        std::cin >> value;
        list.push_back(value);
    }
    bTree.create(list);
    int deleteCount;
    std::cin >> deleteCount;
    while (deleteCount--) {
        int deleteValue;
        std::cin >> deleteValue;
        try {
            bTree.remove(bTree.getRoot(), deleteValue);
            bTree.preorder(bTree.getRoot());
            std::cout << std::endl;
            bTree.inorder(bTree.getRoot());
            std::cout << std::endl;
        } catch (const std::out_of_range &e) {
            std::cout << e.what() << std::endl;
        }
    }
    return 0;
}
